﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.APIs.ADS;
using Waker.Entitys;

namespace Waker.Temp
{
    public partial class ADSDemo : Form
    {

        public ADSHalDataStruct.ADS_Comadapter m_comAdatpter = new ADSHalDataStruct.ADS_Comadapter();
        public ADSHalDataStruct.ADS_CommunicationParameter m_comm = new ADSHalDataStruct.ADS_CommunicationParameter();
        public ADSHalDataStruct.ADS_ControllerInformation[] m_controllers = new ADSHalDataStruct.ADS_ControllerInformation[256];
        public bool m_bConnected = false;
        public ADSHalDataStruct.ADS_TimePeriodGroup m_timePeriodGroup = new ADSHalDataStruct.ADS_TimePeriodGroup();
        public int m_nCurSelTimeGroup = 0;
        public ADSHalDataStruct.ADS_LogicSubDeviceInformation[] m_LogicSubDeviceInfo = new ADSHalDataStruct.ADS_LogicSubDeviceInformation[128];
        public ADSHalDataStruct.ADS_LogicSubDeviceInformation m_selLogicSubDeviceInfo = new ADSHalDataStruct.ADS_LogicSubDeviceInformation();

        public uint m_uLogSudeviceCount = 0;
        public ADSHalDataStruct.ADS_IoInformation[] m_IOInfo = new ADSHalDataStruct.ADS_IoInformation[32];
        public ADSHalDataStruct.ADS_IoInformation m_curIOInfo = new ADSHalDataStruct.ADS_IoInformation();
        public int m_nCurSelIO = -1;

        public ADSDemo()
        {
            InitializeComponent();

            for (int i = 0; i < 256; i++)
            {
                m_controllers[i].Init();
            }

            for (int i = 0; i < 128; i++)
            {
                m_LogicSubDeviceInfo[i].Init();
            }

            for (int i = 0; i < 32; i++)
            {
                m_IOInfo[i].Init();
            }

            m_comAdatpter.address = 0;
            m_comAdatpter.type = (byte)ADSHalConstant.ADS_COMAdapterType.ADS_ADT_TCP;
            m_comAdatpter.port = 0;
            comboBox1.SelectedIndex = 0;
            textBox1.Text = "192.168.1.210";
            textBox2.Text = "65000";
            textBox3.Text = "0";

            // 初始化时间组
            m_timePeriodGroup.Init();


            // 初始化通信时段
            m_timePeriodGroup.ID = 1;
            m_timePeriodGroup.holidayGroupID = 0;
            m_timePeriodGroup.count = 10;

            m_timePeriodGroup.timePeriods[0].startDate.year = 0;
            m_timePeriodGroup.timePeriods[0].startDate.month = 1;
            m_timePeriodGroup.timePeriods[0].startDate.day = 1;

            m_timePeriodGroup.timePeriods[0].endDate.year = 99;
            m_timePeriodGroup.timePeriods[0].endDate.month = 12;
            m_timePeriodGroup.timePeriods[0].endDate.day = 31;

            m_timePeriodGroup.timePeriods[0].startTimes[0].hour = 0;
            m_timePeriodGroup.timePeriods[0].startTimes[0].minute = 0;
            m_timePeriodGroup.timePeriods[0].startTimes[0].sec = 0;

            m_timePeriodGroup.timePeriods[0].endTimes[0].hour = 23;
            m_timePeriodGroup.timePeriods[0].endTimes[0].minute = 59;
            m_timePeriodGroup.timePeriods[0].endTimes[0].sec = 59;

            m_timePeriodGroup.timePeriods[0].validWeek = 0x7F;

            for (int i = 1; i < 10; i++)
            {
                m_timePeriodGroup.timePeriods[i].startDate.month = 1;
                m_timePeriodGroup.timePeriods[i].startDate.day = 1;
                m_timePeriodGroup.timePeriods[i].endDate.month = 1;
                m_timePeriodGroup.timePeriods[i].endDate.day = 1;
            }

            listBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 1;
            comboBox3.SelectedIndex = 1;
            comboBox4.SelectedIndex = 1;
            comboBox5.SelectedIndex = 1;
            comboBox6.SelectedIndex = 1;
            comboBox7.SelectedIndex = 1;
        }

        // 搜索设备
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            IntPtr[] ptArray = new IntPtr[1];
            ptArray[0] = System.Runtime.InteropServices.Marshal.AllocHGlobal(
                System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_ControllerInformation)) * 256);
            IntPtr pt = System.Runtime.InteropServices.Marshal.AllocHGlobal(
                System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_ControllerInformation)));
            System.Runtime.InteropServices.Marshal.Copy(ptArray, 0, pt, 1);

            uint uRetCount = 0;
            int iResult = ADSHalAPI.ADS_SearchController(ref m_comAdatpter, 1, 256, pt, 256, ref uRetCount);
            if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS && uRetCount > 0)
            {
                for (int j = 0; j < uRetCount; j++)
                {
                    m_controllers[j] =
                    (ADSHalDataStruct.ADS_ControllerInformation)System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)((UInt32)pt + j * System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_ControllerInformation)))
                    , typeof(ADSHalDataStruct.ADS_ControllerInformation));
                }

                for (uint uIndex = 0; uIndex < uRetCount; uIndex++)
                {
                    ListViewItem li = new ListViewItem();
                    li.SubItems.Clear();
                    li.SubItems[0].Text = (uIndex + 1).ToString();

                    li.SubItems.Add(ADSHelp.Int2IP(m_controllers[uIndex].commParam.deviceAddr));
                    li.SubItems.Add(m_controllers[uIndex].deviceID.ToString());

                    listView1.Items.Add(li);
                }
            }
            System.Runtime.InteropServices.Marshal.FreeHGlobal(ptArray[0]);
            System.Runtime.InteropServices.Marshal.FreeHGlobal(pt);
        }

        private void SelectController(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }

            int nSelect = listView1.SelectedItems[0].Index;
            if (nSelect >= 0)
            {
                textBox1.Text = ADSHelp.Int2IP(m_controllers[nSelect].commParam.deviceAddr);
                textBox2.Text = m_controllers[nSelect].commParam.devicePort.ToString();
                textBox3.Text = (0).ToString();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(m_controllers[nSelect].description);

                textBox4.Text = m_controllers[nSelect].deviceID.ToString();
                textBox5.Text = sb.ToString();
                textBox6.Text = m_controllers[nSelect].firmwareVersion.ToString();
                textBox7.Text = sb.ToString();
                textBox8.Text = m_controllers[nSelect].maxCardHolderCount.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            m_bConnected = false;
            // 连接
            m_comm.deviceAddr = ADSHelp.IP2Int(textBox1.Text);
            m_comm.devicePort = (ushort)(Convert.ToUInt16(textBox2.Text));
            m_comm.password = (ushort)(Convert.ToUInt16(textBox3.Text));

            int iResult = ADSHalAPI.ADS_ConnectController(ref m_comAdatpter, ref m_comm);
            ADSHelp.PromptResult(iResult, true);

            if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
            {
                this.Text = "连接到:" + textBox1.Text;
                m_bConnected = true;
            }
        }

        // 断开连接
        private void button4_Click(object sender, EventArgs e)
        {
            int iResult = ADSHalAPI.ADS_DisconnectController(ref m_comAdatpter, ref m_comm);
            ADSHelp.PromptResult(iResult, true);

            if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
            {
                this.Text = "门禁Demo";
                m_bConnected = false;
            }
        }

        // 获取控制器数据
        private void button5_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_ControllerInformation ctrlInfo = new ADSHalDataStruct.ADS_ControllerInformation();
                int iResult = ADSHalAPI.ADS_GetControllerInformation(ref m_comAdatpter, ref m_comm, ref ctrlInfo);
                ADSHelp.PromptResult(iResult, true);

                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append(ctrlInfo.description);

                    textBox4.Text = ctrlInfo.deviceID.ToString();
                    textBox5.Text = sb.ToString();
                    textBox6.Text = ctrlInfo.firmwareVersion.ToString();
                    textBox7.Text = sb.ToString();
                    textBox8.Text = ctrlInfo.maxCardHolderCount.ToString();
                }
            }
        }

        // 复位
        private void button6_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_ResetController(ref m_comAdatpter, ref m_comm);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取时钟
        private void button7_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_YMDHMS time = new ADSHalDataStruct.ADS_YMDHMS();

                int iResult = ADSHalAPI.ADS_GetTime(ref m_comAdatpter, ref m_comm, ref time);
                ADSHelp.PromptResult(iResult, true);
                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    dateTimePicker1.Value = new DateTime(time.year + 2000, time.month, time.day, time.hour, time.minute, time.sec);
                    dateTimePicker2.Value = new DateTime(time.year + 2000, time.month, time.day, time.hour, time.minute, time.sec);
                }
            }
        }

        // 校时
        private void button8_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_YMDHMS time = new ADSHalDataStruct.ADS_YMDHMS();

                time.year = (byte)(dateTimePicker1.Value.Year - 2000);
                time.month = (byte)dateTimePicker1.Value.Month;
                time.day = (byte)dateTimePicker1.Value.Day;
                time.hour = (byte)dateTimePicker2.Value.Hour;
                time.minute = (byte)dateTimePicker2.Value.Minute;
                time.sec = (byte)dateTimePicker1.Value.Second;

                int iResult = ADSHalAPI.ADS_SetTime(ref m_comAdatpter, ref m_comm, ref time);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 格式化控制器
        private void button10_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_FormatController(ref m_comAdatpter, ref m_comm, 20000);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now;
        }

        // 设置通行时段
        private void button11_Click(object sender, EventArgs e)
        {
            SaveCurTimePeriod();

            m_timePeriodGroup.ID = (byte)(Convert.ToUInt16(textBox9.Text));
            m_timePeriodGroup.holidayGroupID = (byte)(Convert.ToUInt16(textBox22.Text));

            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_SetTimePeriodGroup(ref m_comAdatpter, ref m_comm, ref m_timePeriodGroup);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 删除通行时段
        private void button12_Click(object sender, EventArgs e)
        {
            m_timePeriodGroup.ID = (byte)(Convert.ToUInt16(textBox9.Text));
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_DeleteTimePeriodGroup(ref m_comAdatpter, ref m_comm, m_timePeriodGroup.ID);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取通信时段
        private void button13_Click(object sender, EventArgs e)
        {
            m_timePeriodGroup.ID = (byte)(Convert.ToUInt16(textBox9.Text));
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_GetTimePeriodGroup(ref m_comAdatpter, ref m_comm, ref m_timePeriodGroup);
                ADSHelp.PromptResult(iResult, true);
                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    UpdateTimeGroup();
                }
            }
        }

        // 清空所有通行时间段
        private void button14_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_ClearTimePeriodGroup(ref m_comAdatpter, ref m_comm);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 保存当前时间段
        private void SaveCurTimePeriod()
        {
            int nSel = m_nCurSelTimeGroup;
            m_timePeriodGroup.timePeriods[nSel].startDate.year = (byte)(dateTimePicker3.Value.Year - 2000);
            m_timePeriodGroup.timePeriods[nSel].startDate.month = (byte)dateTimePicker3.Value.Month;
            m_timePeriodGroup.timePeriods[nSel].startDate.day = (byte)dateTimePicker3.Value.Day;

            m_timePeriodGroup.timePeriods[nSel].endDate.year = (byte)(dateTimePicker4.Value.Year - 2000);
            m_timePeriodGroup.timePeriods[nSel].endDate.month = (byte)dateTimePicker4.Value.Month;
            m_timePeriodGroup.timePeriods[nSel].endDate.day = (byte)dateTimePicker4.Value.Day;

            m_timePeriodGroup.timePeriods[nSel].startTimes[0].hour = (byte)dateTimePicker6.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].startTimes[0].minute = (byte)dateTimePicker6.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].startTimes[0].sec = (byte)dateTimePicker6.Value.Second;
            m_timePeriodGroup.timePeriods[nSel].endTimes[0].hour = (byte)dateTimePicker5.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].endTimes[0].minute = (byte)dateTimePicker5.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].endTimes[0].sec = (byte)dateTimePicker5.Value.Second;

            m_timePeriodGroup.timePeriods[nSel].startTimes[1].hour = (byte)dateTimePicker8.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].startTimes[1].minute = (byte)dateTimePicker8.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].startTimes[1].sec = (byte)dateTimePicker8.Value.Second;
            m_timePeriodGroup.timePeriods[nSel].endTimes[1].hour = (byte)dateTimePicker7.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].endTimes[1].minute = (byte)dateTimePicker7.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].endTimes[1].sec = (byte)dateTimePicker7.Value.Second;

            m_timePeriodGroup.timePeriods[nSel].startTimes[2].hour = (byte)dateTimePicker10.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].startTimes[2].minute = (byte)dateTimePicker10.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].startTimes[2].sec = (byte)dateTimePicker10.Value.Second;
            m_timePeriodGroup.timePeriods[nSel].endTimes[2].hour = (byte)dateTimePicker9.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].endTimes[2].minute = (byte)dateTimePicker9.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].endTimes[2].sec = (byte)dateTimePicker9.Value.Second;

            m_timePeriodGroup.timePeriods[nSel].startTimes[3].hour = (byte)dateTimePicker12.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].startTimes[3].minute = (byte)dateTimePicker12.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].startTimes[3].sec = (byte)dateTimePicker12.Value.Second;
            m_timePeriodGroup.timePeriods[nSel].endTimes[3].hour = (byte)dateTimePicker11.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].endTimes[3].minute = (byte)dateTimePicker11.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].endTimes[3].sec = (byte)dateTimePicker11.Value.Second;

            m_timePeriodGroup.timePeriods[nSel].startTimes[4].hour = (byte)dateTimePicker14.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].startTimes[4].minute = (byte)dateTimePicker14.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].startTimes[4].sec = (byte)dateTimePicker14.Value.Second;
            m_timePeriodGroup.timePeriods[nSel].endTimes[4].hour = (byte)dateTimePicker14.Value.Hour;
            m_timePeriodGroup.timePeriods[nSel].endTimes[4].minute = (byte)dateTimePicker14.Value.Minute;
            m_timePeriodGroup.timePeriods[nSel].endTimes[4].sec = (byte)dateTimePicker14.Value.Second;

            m_timePeriodGroup.timePeriods[nSel].validWeek = 0;
            if (checkBox1.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_MON;
            }

            if (checkBox2.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES;
            }

            if (checkBox3.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES;
            }

            if (checkBox4.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS;
            }

            if (checkBox5.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI;
            }

            if (checkBox6.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR;
            }

            if (checkBox7.Checked)
            {
                m_timePeriodGroup.timePeriods[nSel].validWeek |= (byte)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN;
            }
        }

        private void UpdateTimeGroup()
        {
            int nSel = m_nCurSelTimeGroup;

            dateTimePicker3.Value = new DateTime(m_timePeriodGroup.timePeriods[nSel].startDate.year + 2000,
                m_timePeriodGroup.timePeriods[nSel].startDate.month,
                m_timePeriodGroup.timePeriods[nSel].startDate.day, 0, 0, 0);
            dateTimePicker4.Value = new DateTime(m_timePeriodGroup.timePeriods[nSel].endDate.year + 2000,
                m_timePeriodGroup.timePeriods[nSel].endDate.month,
                m_timePeriodGroup.timePeriods[nSel].endDate.day, 0, 0, 0);

            dateTimePicker6.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].startTimes[0].hour,
                m_timePeriodGroup.timePeriods[nSel].startTimes[0].minute,
                m_timePeriodGroup.timePeriods[nSel].startTimes[0].sec);
            dateTimePicker5.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].endTimes[0].hour,
                m_timePeriodGroup.timePeriods[nSel].endTimes[0].minute,
                m_timePeriodGroup.timePeriods[nSel].endTimes[0].sec);

            dateTimePicker8.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].startTimes[1].hour,
                m_timePeriodGroup.timePeriods[nSel].startTimes[1].minute,
                m_timePeriodGroup.timePeriods[nSel].startTimes[1].sec);
            dateTimePicker7.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].endTimes[1].hour,
                m_timePeriodGroup.timePeriods[nSel].endTimes[1].minute,
                m_timePeriodGroup.timePeriods[nSel].endTimes[1].sec);

            dateTimePicker10.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].startTimes[2].hour,
                m_timePeriodGroup.timePeriods[nSel].startTimes[2].minute,
                m_timePeriodGroup.timePeriods[nSel].startTimes[2].sec);
            dateTimePicker9.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].endTimes[2].hour,
                m_timePeriodGroup.timePeriods[nSel].endTimes[2].minute,
                m_timePeriodGroup.timePeriods[nSel].endTimes[2].sec);

            dateTimePicker12.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].startTimes[3].hour,
                m_timePeriodGroup.timePeriods[nSel].startTimes[3].minute,
                m_timePeriodGroup.timePeriods[nSel].startTimes[3].sec);
            dateTimePicker11.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].endTimes[3].hour,
                m_timePeriodGroup.timePeriods[nSel].endTimes[3].minute,
                m_timePeriodGroup.timePeriods[nSel].endTimes[3].sec);

            dateTimePicker14.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].startTimes[4].hour,
                m_timePeriodGroup.timePeriods[nSel].startTimes[4].minute,
                m_timePeriodGroup.timePeriods[nSel].startTimes[4].sec);
            dateTimePicker13.Value = new DateTime(2000, 1, 1,
                m_timePeriodGroup.timePeriods[nSel].endTimes[4].hour,
                m_timePeriodGroup.timePeriods[nSel].endTimes[4].minute,
                m_timePeriodGroup.timePeriods[nSel].endTimes[4].sec);

            textBox22.Text = m_timePeriodGroup.holidayGroupID.ToString();

            checkBox1.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_MON)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_MON;
            checkBox2.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_TUES;
            checkBox3.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_WEDNES;
            checkBox4.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_THURS;
            checkBox5.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_FRI;
            checkBox6.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SATUR;
            checkBox7.Checked = (int)((int)m_timePeriodGroup.timePeriods[nSel].validWeek & (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN)
                    == (int)ADSHalConstant.ADS_WeekMask.ADS_WM_SUN;
        }

        // 改变选择时间组
        private void ChangeTimeGroup(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count <= 0)
            {
                return;
            }

            // 先保存
            SaveCurTimePeriod();

            int nSel = listBox1.SelectedIndex;
            m_nCurSelTimeGroup = nSel;
            UpdateTimeGroup();
        }

        // 设置部门
        private void button18_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Department dept = new ADSHalDataStruct.ADS_Department();
                dept.superiorID = (byte)(Convert.ToUInt16(textBox10.Text));
                dept.ID = (byte)(Convert.ToUInt16(textBox11.Text));
                int iResult = ADSHalAPI.ADS_SetDepartment(ref m_comAdatpter, ref m_comm, ref dept);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 删除部门
        private void button17_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Department dept = new ADSHalDataStruct.ADS_Department();
                dept.superiorID = (byte)(Convert.ToUInt16(textBox10.Text));
                dept.ID = (byte)(Convert.ToUInt16(textBox11.Text));
                int iResult = ADSHalAPI.ADS_DeleteDepartment(ref m_comAdatpter, ref m_comm, dept.ID);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取部门
        private void button16_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Department dept = new ADSHalDataStruct.ADS_Department();
                dept.ID = (byte)(Convert.ToUInt16(textBox11.Text));
                int iResult = ADSHalAPI.ADS_GetDepartment(ref m_comAdatpter, ref m_comm, ref dept);
                ADSHelp.PromptResult(iResult, true);
                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    textBox10.Text = dept.superiorID.ToString();
                }
            }
        }

        // 清除部门
        private void button15_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_ClearDepartment(ref m_comAdatpter, ref m_comm);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 设置卡片
        private void button22_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_CardHolder card = new ADSHalDataStruct.ADS_CardHolder();
                card.cardNumber.LoNumber = Convert.ToUInt32(textBox12.Text);
                card.cardNumber.HiNumber = Convert.ToUInt32(textBox13.Text);
                card.password = Convert.ToUInt32(textBox14.Text);
                card.departmentID = Convert.ToUInt16(textBox15.Text);
                card.groupNumber = Convert.ToByte(textBox16.Text);
                card.expirationDate.year = (byte)(dateTimePicker15.Value.Year - 2000);
                card.expirationDate.month = (byte)dateTimePicker15.Value.Month;
                card.expirationDate.day = (byte)dateTimePicker15.Value.Day;
                card.expirationDate.hour = (byte)dateTimePicker16.Value.Hour;
                card.expirationDate.minute = (byte)dateTimePicker16.Value.Minute;
                card.expirationDate.sec = (byte)dateTimePicker16.Value.Second;
                uint uRetCount = 0;

                int iResult = ADSHalAPI.ADS_SetCardHolders(ref m_comAdatpter, ref m_comm, ref card, 1, ref uRetCount);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 删除卡片
        private void button21_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_CardHolder card = new ADSHalDataStruct.ADS_CardHolder();
                card.cardNumber.LoNumber = Convert.ToUInt32(textBox12.Text);
                card.cardNumber.HiNumber = Convert.ToUInt32(textBox13.Text);

                int iResult = ADSHalAPI.ADS_DeleteCardHolder(ref m_comAdatpter, ref m_comm, ref card);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取卡片
        private void button20_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_CardHolder card = new ADSHalDataStruct.ADS_CardHolder();
                card.cardNumber.LoNumber = Convert.ToUInt32(textBox12.Text);
                card.cardNumber.HiNumber = Convert.ToUInt32(textBox13.Text);

                int iResult = ADSHalAPI.ADS_GetCardHolder(ref m_comAdatpter, ref m_comm, ref card);
                ADSHelp.PromptResult(iResult, true);

                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    textBox14.Text = card.password.ToString();
                    textBox15.Text = card.departmentID.ToString();
                    textBox16.Text = card.groupNumber.ToString();
                    dateTimePicker15.Value = new DateTime(card.expirationDate.year + 2000, card.expirationDate.month, card.expirationDate.day, 0, 0, 0);
                    dateTimePicker16.Value = new DateTime(card.expirationDate.year + 2000, card.expirationDate.month, card.expirationDate.day,
                        card.expirationDate.hour, card.expirationDate.minute, card.expirationDate.sec);
                }
            }
        }

        // 设置权限
        private void button26_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Permission permission = new ADSHalDataStruct.ADS_Permission();

                permission.ID = Convert.ToUInt32(textBox17.Text);
                permission.logicSubDeviceAddress.physicalSubDevAddr = 0;
                permission.logicSubDeviceAddress.logicSubDevNumber = Convert.ToByte(textBox18.Text);
                permission.timePeriodGroupID = Convert.ToByte(textBox19.Text);

                // 部门
                if (radioButton1.Checked)
                {
                    permission.isCardNumber = 0;
                    permission.departID = Convert.ToUInt16(textBox20.Text);
                }
                else // 卡片
                {
                    permission.isCardNumber = 1;
                    permission.cardNumber.LoNumber = Convert.ToUInt32(textBox21.Text);
                    permission.cardNumber.HiNumber = Convert.ToUInt32(textBox23.Text);
                }

                permission.readerPos = (byte)comboBox2.SelectedIndex;

                permission.actionPortMask = 0;
                permission.actionPortState = 0;

                if (checkBox8.Checked)
                {
                    permission.actionPortMask |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK;
                    if (comboBox3.SelectedIndex == 1)
                    {
                        permission.actionPortState |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK;
                    }
                }

                if (checkBox9.Checked)
                {
                    permission.actionPortMask |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1;
                    if (comboBox4.SelectedIndex == 1)
                    {
                        permission.actionPortState |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1;
                    }
                }

                if (checkBox10.Checked)
                {
                    permission.actionPortMask |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2;
                    if (comboBox5.SelectedIndex == 1)
                    {
                        permission.actionPortState |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2;
                    }
                }

                if (checkBox11.Checked)
                {
                    permission.actionPortMask |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3;
                    if (comboBox6.SelectedIndex == 1)
                    {
                        permission.actionPortState |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3;
                    }
                }

                if (checkBox12.Checked)
                {
                    permission.actionPortMask |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4;
                    if (comboBox7.SelectedIndex == 1)
                    {
                        permission.actionPortState |= (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4;
                    }
                }

                int iResult = ADSHalAPI.ADS_SetPermission(ref m_comAdatpter, ref m_comm, ref permission);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 删除权限
        private void button25_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_DeletePermission(ref m_comAdatpter, ref m_comm, Convert.ToUInt32(textBox17.Text));
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取权限
        private void button24_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Permission permission = new ADSHalDataStruct.ADS_Permission();

                permission.ID = Convert.ToUInt32(textBox17.Text);
                int iResult = ADSHalAPI.ADS_GetPermission(ref m_comAdatpter, ref m_comm, ref permission);
                ADSHelp.PromptResult(iResult, true);
                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    textBox18.Text = permission.logicSubDeviceAddress.logicSubDevNumber.ToString();
                    textBox19.Text = permission.timePeriodGroupID.ToString();
                    if (permission.isCardNumber == 1)
                    {
                        radioButton1.Checked = false;
                        radioButton2.Checked = true;
                        textBox21.Text = permission.cardNumber.LoNumber.ToString();
                        textBox23.Text = permission.cardNumber.HiNumber.ToString();
                        textBox20.Text = "0";
                    }
                    else
                    {
                        radioButton1.Checked = true;
                        radioButton2.Checked = false;
                        textBox20.Text = permission.departID.ToString();
                        textBox21.Text = "0";
                        textBox23.Text = "0";
                    }

                    comboBox2.SelectedIndex = permission.readerPos;
                    checkBox8.Checked = (uint)(permission.actionPortMask & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK)
                                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK;
                    comboBox3.SelectedIndex = (uint)(permission.actionPortState & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK)
                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK ? 1 : 0;

                    checkBox9.Checked = (uint)(permission.actionPortMask & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1)
                                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1;
                    comboBox4.SelectedIndex = (uint)(permission.actionPortState & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1)
                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT1 ? 1 : 0;

                    checkBox10.Checked = (uint)(permission.actionPortMask & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2)
                                       == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2;
                    comboBox5.SelectedIndex = (uint)(permission.actionPortState & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2)
                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT2 ? 1 : 0;

                    checkBox11.Checked = (uint)(permission.actionPortMask & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3)
                                       == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3;
                    comboBox6.SelectedIndex = (uint)(permission.actionPortState & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3)
                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT3 ? 1 : 0;

                    checkBox12.Checked = (uint)(permission.actionPortMask & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4)
                                       == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4;
                    comboBox7.SelectedIndex = (uint)(permission.actionPortState & (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4)
                        == (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_AUX_OUT4 ? 1 : 0;
                }

            }
        }

        // 清空权限
        private void button23_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                int iResult = ADSHalAPI.ADS_ClearPermission(ref m_comAdatpter, ref m_comm);
                ADSHelp.PromptResult(iResult, true);
            }
        }

        // 获取事件
        private void button27_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_Event[] acsevent = new ADSHalDataStruct.ADS_Event[100];
                uint uRetCount = 0;

                int iResult = ADSHalAPI.ADS_ReadEvents(ref m_comAdatpter, ref m_comm, ref acsevent[0], 100, ref uRetCount);
                ADSHelp.PromptResult(iResult, true);

                if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS && uRetCount > 0)
                {
                    for (int i = 0; i < uRetCount; i++)
                    {
                        int nItem = listView2.Items.Count;
                        ListViewItem li = new ListViewItem();
                        li.SubItems.Clear();
                        li.SubItems[0].Text = (nItem + 1).ToString();
                        li.SubItems.Add(ADSHelp.Int2IP(m_comm.deviceAddr));
                        li.SubItems.Add(acsevent[i].logicSubDeviceAddress.logicSubDevNumber.ToString());
                        li.SubItems.Add(acsevent[i].cardNumber.LoNumber.ToString() + "-" + acsevent[i].cardNumber.HiNumber.ToString());
                        li.SubItems.Add((acsevent[i].time.year + 2000).ToString() + "-" +
                                        acsevent[i].time.month.ToString() + "-" +
                                        acsevent[i].time.day.ToString() + " " +
                                        acsevent[i].time.hour.ToString() + ":" +
                                        acsevent[i].time.minute.ToString() + ":" +
                                        acsevent[i].time.sec.ToString());
                        li.SubItems.Add(ADSHelp.GetEventName((ADSHalConstant.ADS_EventType)acsevent[i].type));
                        if (acsevent[i].accessBlockedReason != 0)
                        {
                            li.SubItems.Add(ADSHalAPI.ADS_Helper_GetErrorMessage(acsevent[i].accessBlockedReason));
                        }
                        else
                        {
                            li.SubItems.Add("");
                        }

                        listView2.Items.Add(li);
                    }

                    // 设置已经读取的事件数，防止重复读取
                    ADSHalAPI.ADS_IncreaseEventCount(ref m_comAdatpter, ref m_comm, uRetCount);
                }
            }
        }

        // 清空事件列表
        private void button28_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();
        }

        // 搜索子设备
        private void button19_Click(object sender, EventArgs e)
        {
            listView3.Items.Clear();
            if (m_bConnected)
            {
                m_uLogSudeviceCount = 0;
                /*
                   搜索子设备流程：搜索物理子设备，搜索逻辑子设备
                */
                ADSHalDataStruct.ADS_PhysicalSubDeviceInformation[] physicalSubDevices = new ADSHalDataStruct.ADS_PhysicalSubDeviceInformation[64];

                uint uRetCount = 0;

                // 搜索物理子设备
                int iResult = ADSHalAPI.ADS_SearchPhysicalSubDevices(ref m_comAdatpter, ref m_comm, ref physicalSubDevices[0], 64, ref uRetCount);

                if (iResult != (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                {
                    ADSHelp.PromptResult(iResult, true);
                    return;
                }

                //string[] logicSubDeviceType = { "本地门点", "远程门点", "本地简单IO", "远程简单IO", "本地电梯", "远程电梯", "本地报警", "远程报警" };

                // 搜索逻辑子设备
                uint uLogicCount = 0;

                for (uint uIndex = 0; uIndex < uRetCount; uIndex++)
                {
                    uLogicCount = 0;

                    ADSHalDataStruct.ADS_PhysicalSubDeviceAddress phyphysicalAddr = new ADSHalDataStruct.ADS_PhysicalSubDeviceAddress();
                    phyphysicalAddr.physicalSubDevAddr = physicalSubDevices[uIndex].subDevAddr;
                    phyphysicalAddr.physicalSubDevID = physicalSubDevices[uIndex].physicalSubDevID;

                    IntPtr[] ptArray = new IntPtr[1];
                    ptArray[0] = System.Runtime.InteropServices.Marshal.AllocHGlobal(
                        System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_LogicSubDeviceInformation)) * 32);
                    IntPtr pt = System.Runtime.InteropServices.Marshal.AllocHGlobal(
                        System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_LogicSubDeviceInformation)));
                    System.Runtime.InteropServices.Marshal.Copy(ptArray, 0, pt, 1);

                    iResult = ADSHalAPI.ADS_SearchLogicSubDevices(ref m_comAdatpter, ref m_comm, ref phyphysicalAddr, pt,
                    32, ref uLogicCount);

                    for (int j = 0; j < uLogicCount; j++)
                    {
                        m_LogicSubDeviceInfo[j + m_uLogSudeviceCount] =
                        (ADSHalDataStruct.ADS_LogicSubDeviceInformation)System.Runtime.InteropServices.Marshal.PtrToStructure((IntPtr)((UInt32)pt + j * System.Runtime.InteropServices.Marshal.SizeOf(typeof(ADSHalDataStruct.ADS_LogicSubDeviceInformation)))
                        , typeof(ADSHalDataStruct.ADS_LogicSubDeviceInformation));
                    }
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(ptArray[0]);
                    System.Runtime.InteropServices.Marshal.FreeHGlobal(pt);

                    for (uint i = 0; i < uLogicCount; i++)
                    {
                        ListViewItem li = new ListViewItem();
                        li.SubItems.Clear();
                        li.SubItems[0].Text = (m_LogicSubDeviceInfo[i + m_uLogSudeviceCount].logicSubDeviceAddrNumber.physicalSubDevAddr).ToString();
                        li.SubItems.Add(m_LogicSubDeviceInfo[i + m_uLogSudeviceCount].logicSubDeviceAddrNumber.logicSubDevNumber.ToString());
                        li.SubItems.Add(ADSHelp.logicSubDeviceType[m_LogicSubDeviceInfo[i + m_uLogSudeviceCount].logicSubDeviceCategory - 1]);
                        listView3.Items.Add(li);
                    }

                    m_uLogSudeviceCount += uLogicCount;
                }
            }
        }

        // 清除子设备
        private void button29_Click(object sender, EventArgs e)
        {
            listView3.Items.Clear();
        }

        // 选择子设备
        private void SelectSubDevice(object sender, EventArgs e)
        {
            if (listView3.SelectedItems.Count == 0)
            {
                return;
            }

            int nSelect = listView3.SelectedItems[0].Index;
            if (nSelect >= 0)
            {
                textBox24.Text = m_LogicSubDeviceInfo[nSelect].logicSubDeviceAddrNumber.physicalSubDevAddr.ToString();
                textBox25.Text = m_LogicSubDeviceInfo[nSelect].logicSubDeviceAddrNumber.logicSubDevNumber.ToString();

                m_selLogicSubDeviceInfo = m_LogicSubDeviceInfo[nSelect];

                int nCount = 0;

                // 列出节点
                if (m_selLogicSubDeviceInfo.logicSubDeviceCategory == (byte)ADSHalConstant.ADS_LogicSubDeviceCategory.ADS_LSDC_LOCAL_DOOR)
                {
                    nCount = 3;
                    // 只有门锁和按钮、门磁
                    m_IOInfo[0].ioName[0] = '门';
                    m_IOInfo[0].ioName[1] = '磁';
                    m_IOInfo[0].ioNumber = ADSHalConstant.ADS_IOM_DOOR;
                    m_IOInfo[0].nIsAction = 0;

                    m_IOInfo[1].ioName[0] = '开';
                    m_IOInfo[1].ioName[1] = '门';
                    m_IOInfo[1].ioName[2] = '按';
                    m_IOInfo[1].ioName[3] = '纽';
                    m_IOInfo[1].ioNumber = ADSHalConstant.ADS_IOM_BUTTON;
                    m_IOInfo[1].nIsAction = 0;

                    m_IOInfo[2].ioName[0] = '电';
                    m_IOInfo[2].ioName[1] = '锁';
                    m_IOInfo[2].ioNumber = (uint)ADSHalConstant.ADS_IoNumber.ADS_IOM_LOCK;
                    m_IOInfo[2].nIsAction = 1;
                }

                listView4.Items.Clear();
                for (int i = 0; i < nCount; i++)
                {
                    ListViewItem li = new ListViewItem();
                    li.SubItems.Clear();

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append(m_IOInfo[i].ioName);

                    li.SubItems[0].Text = sb.ToString();
                    li.SubItems.Add("");
                    li.SubItems.Add("");
                    listView4.Items.Add(li);
                }
            }
        }

        // 获取门点状态
        private void button30_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_LogicSubDeviceAddress subAddress = new ADSHalDataStruct.ADS_LogicSubDeviceAddress();
                subAddress.physicalSubDevAddr = Convert.ToByte(textBox24.Text);
                subAddress.logicSubDevNumber = Convert.ToByte(textBox25.Text);

                if (subAddress.logicSubDevNumber != 0)
                {
                    uint uOnLine = 0;
                    uint uState = 0;
                    int iResult = ADSHalAPI.ADS_GetLogicSubDeviceState(ref m_comAdatpter, ref m_comm, ref subAddress, ref uOnLine, ref uState);
                    ADSHelp.PromptResult(iResult, true);

                    if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                    {
                        if (uOnLine == 0)
                        {
                            textBox26.Text = "离线";
                        }
                        else
                        {
                            textBox26.Text = "在线-" + ((uState == 1) ? "门开" : "门关");
                        }
                    }
                }
            }
        }

        // 选择端口
        private void SelectIO(object sender, EventArgs e)
        {
            if (listView4.SelectedItems.Count == 0)
            {
                return;
            }

            int nSelect = listView4.SelectedItems[0].Index;
            if (nSelect >= 0)
            {
                m_curIOInfo = m_IOInfo[nSelect];
                m_nCurSelIO = nSelect;
            }
        }

        // 打开端口
        private void button31_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_LogicSubDeviceAddress subAddress = new ADSHalDataStruct.ADS_LogicSubDeviceAddress();
                subAddress.physicalSubDevAddr = Convert.ToByte(textBox24.Text);
                subAddress.logicSubDevNumber = Convert.ToByte(textBox25.Text);

                if (subAddress.logicSubDevNumber != 0 && m_curIOInfo.ioNumber != 0 && m_curIOInfo.nIsAction == 1)
                {
                    ADSHalDataStruct.ADS_IoAddress ioAddress = new ADSHalDataStruct.ADS_IoAddress();
                    ioAddress.ioNumber = m_curIOInfo.ioNumber;
                    int iResult = ADSHalAPI.ADS_OpenOputputIo(ref m_comAdatpter, ref m_comm, ref subAddress, ref ioAddress);
                    ADSHelp.PromptResult(iResult, true);
                }
            }
        }

        // 关闭
        private void button32_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_LogicSubDeviceAddress subAddress = new ADSHalDataStruct.ADS_LogicSubDeviceAddress();
                subAddress.physicalSubDevAddr = Convert.ToByte(textBox24.Text);
                subAddress.logicSubDevNumber = Convert.ToByte(textBox25.Text);

                if (subAddress.logicSubDevNumber != 0 && m_curIOInfo.ioNumber != 0 && m_curIOInfo.nIsAction == 1)
                {
                    ADSHalDataStruct.ADS_IoAddress ioAddress = new ADSHalDataStruct.ADS_IoAddress();
                    ioAddress.ioNumber = m_curIOInfo.ioNumber;
                    int iResult = ADSHalAPI.ADS_CloseOputputIo(ref m_comAdatpter, ref m_comm, ref subAddress, ref ioAddress);
                    ADSHelp.PromptResult(iResult, true);
                }
            }
        }

        // 获取节点状态
        private void button33_Click(object sender, EventArgs e)
        {
            if (m_bConnected)
            {
                ADSHalDataStruct.ADS_LogicSubDeviceAddress subAddress = new ADSHalDataStruct.ADS_LogicSubDeviceAddress();
                subAddress.physicalSubDevAddr = Convert.ToByte(textBox24.Text);
                subAddress.logicSubDevNumber = Convert.ToByte(textBox25.Text);
                ADSHalDataStruct.ADS_IoAddress ioAddress = new ADSHalDataStruct.ADS_IoAddress();
                ioAddress.ioNumber = m_curIOInfo.ioNumber;

                if (subAddress.logicSubDevNumber != 0 && m_curIOInfo.ioNumber != 0)
                {
                    uint uState = 0;

                    int iResult = ADSHalAPI.ADS_GetIoState(ref m_comAdatpter, ref m_comm, ref subAddress, ref ioAddress, ref uState);
                    ADSHelp.PromptResult(iResult, true);
                    if (iResult == (int)ADSHalConstant.ADS_ResultCode.ADS_RC_SUCCESS)
                    {
                        if (m_curIOInfo.nIsAction == 1)
                        {
                            listView4.Items[m_nCurSelIO].SubItems[1].Text = (uState == 1 ? "打开" : "关闭");
                        }
                        else
                        {
                            listView4.Items[m_nCurSelIO].SubItems[1].Text = (uState == 1 ? "打开" : "关闭");

                            byte uArmState = 0;
                            ADSHalAPI.ADS_GetInputIoArmState(ref m_comAdatpter, ref m_comm, ref subAddress, ref ioAddress, ref uArmState);
                            listView4.Items[m_nCurSelIO].SubItems[2].Text = (uArmState == 1 ? "布防" : "撤防");
                        }
                    }
                }
            }
        }
    }
}
